<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\FourAdminRole;
use App\Models\FourAdminUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class FourAdminRoleController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(\App\Models\FourAdminRole::with('user')->withCount('user'), function (Grid $grid) {
            //角色名称模糊查询
            $grid->quickSearch('role_name');
            $grid->column('role_name','角色名称');
            $grid->column('user_count','角色下管理员人数');
            $grid->column('desc','角色描述');
            $grid->status('是否启用')->switch();
            $grid->column('created_at','添加时间');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FourAdminRole(), function (Show $show) {
            $show->field('id');
            $show->field('role_name','角色名称');
            $show->field('desc','角色描述');
            $show->status('是否启用')->using(['0'=>'未启用','1'=>'已启用']);
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FourAdminRole(), function (Form $form) {
            $form->display('id');
            $form->text('role_name','角色名称');
            $form->text('desc','角色描述');
            $form->switch('status','是否启用')
                ->customFormat(function ($v) {
                    return $v == '1' ? 1 : 0;
                })
                ->saving(function ($v) {
                    return $v ? '1' : '0';
                });
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
